﻿# /proc/cpuinfo : cpuinfo_centos6.5-vb.txt
#安装包在:email0dot0@163.com@http://yunpan.360.cn\待上传

#注意了，本脚本会将python3.4.2安装到/usr/local/Python-3.4.2，
# 将postgresql9.3.5安装到/usr/local/postgresql-9.3.5，如果这些位置已经占用，请先mv成别的名字

# op as user root

pkhm=/usr/cgl/install_config/postgresql9.3.5/pack/
loghm=/usr/cgl/install_config/postgresql9.3.5/log/

#先安装python
cd $pkhm
log=$loghm/python3.log
rm -fr Python-3.4.2 $log && tar -zxf Python-3.4.2.tgz && cd Python-3.4.2
LDFLAGS="-L/usr/local/readline-6.3/lib/" CPPFLAGS="-I/usr/local/readline-6.3/include/" ./configure --prefix=/usr/local/Python-3.4.2 --enable-shared  2>&1 | tee -a $log && \
make 2>&1 | tee -a $log && \
make install 2>&1 | tee -a $log
grep -Hn readline $log && ls -lh /usr/local/Python-3.4.2/lib/*.so
! [ -f /etc/ld.so.conf.d/python3.conf ] && echo /usr/local/Python-3.4.2/lib/ > /etc/ld.so.conf.d/python3.conf && ldconfig && ldconfig -p | grep libpy
ln -s  /usr/local/Python-3.4.2/bin/python3.4 /usr/local/Python-3.4.2/bin/python
export PATH=/usr/local/Python-3.4.2/bin/:$PATH

#setuptools.tar.gz
cd $pkhm
log=$loghm/setuptools.log
rm -fr setuptools-0.9.8 $log && tar -zxf setuptools-0.9.8.tar.gz && cd setuptools-0.9.8
python setup.py install 2>&1 | tee -a $log

#pip.tar.gz
cd $pkhm
log=$loghm/pip.log
rm -fr pip-1.5.6 $log && tar -zxf pip-1.5.6.tar.gz && cd pip-1.5.6
python setup.py install 2>&1 | tee -a $log

#todo{
#uuid-1.6.2.tar.gz
make clean && ./configure --prefix=/usr/local/uuid-1.6.2 && make && make install

# 安装 termcap-1.3.1.tar.gz 和 readline  (realine need termcap)
cd termcap-1.3.1 && ./configure && make && make install
#termcap必须安装在默认位置，不要使用--prefix=/usr/local/termcapXXX，否则 readline 找不到他


cd  readline-6.3 && ./configure  --prefix=/usr/local/readline-6.3 && make && make install
#/sbin/lvm依赖/lib/libreadline.so.xxx，如果把readline安装到默认位置，导致lvm有可能去使用新安装的libreadline，这就有可能导致lvm无法运行，而系统的磁盘分区使用了lvm，
# 所以可能导致系统启动失败
#所以不要把readline安装到默认位置，不要让lvm使用这个新的libreadline

#第一次出现了无法获取/usr/local/readline-6/lib/libhistory.a，再次运行却又没有这个错误了
 #如果不安装 termcap，只安装了readline，则configure postgresql9.3.5时，依然说找不到readline

# termcap 应该和curses是同一种东西
#}

# {用不着这样, 依赖系统自带的readline的东西依然让其用系统自带的readline
# 因为python3在编译的时候已经指定了使用/usr/local/readline-6.3了
#mv /lib/libreadline.so.6.0 /lib/libreadline.so.6.0.original
#unlink /lib/libreadline.so.6
#ln -s  /usr/local/readline-6.3/lib/libreadline.so.6.3  /lib/libreadline.so.6
# }

#请确保使用的的确是python3:
#export PATH=/usr/local/Python-3.4.2/bin/:$PATH
cd $pkhm
log=$loghm/postgresql.log
rm -fr postgresql-9.3.5  $log && tar -jxf postgresql-9.3.5.tar.bz2 && cd postgresql-9.3.5
./configure --prefix=/usr/local/postgresql-9.3.5 --with-ossp-uuid --with-readline --with-libraries="/usr/local/uuid-1.6.2/lib/ /usr/local/readline-6.3/lib /usr/local/Python-3.4.2/lib" --with-includes="/usr/local/uuid-1.6.2/include/ /usr/local/readline-6.3/include /usr/local/Python-3.4.2/include/ " --with-python  2>&1 | tee -a $log && \
make  2>&1 | tee -a $log && \
make install  2>&1 | tee -a $log


ls /usr/local/postgresql-9.3.5/lib/*py*
cd ./contrib/uuid-ossp/
make  2>&1 | tee -a $log && \
make install 2>&1 | tee -a $log

cd $pkhm
adduser postgres
mkdir /usr/local/postgresql-9.3.5/data/
chown -R postgres /usr/local/postgresql-9.3.5/data

su postgres -c "/usr/local/postgresql-9.3.5/bin/initdb -D /usr/local/postgresql-9.3.5/data"
#start db server
su postgres -c "/usr/local/postgresql-9.3.5/bin/postgres -D /usr/local/postgresql-9.3.5/data/ > /usr/local/postgresql-9.3.5/data/postgresql.log 2>&1 &"
 
#create db
su postgres -c "/usr/local/postgresql-9.3.5/bin/createdb -h wildfly.local -p 5437  -U postgres -w test"
 
#connect to db
su postgres -c "/usr/local/postgresql-9.3.5/bin/psql -h wildfly.local -p 5437 -U postgres -w test"
 
#shutdown
su postgres -c "/usr/local/postgresql-9.3.5/bin/pg_ctl stop -D /usr/local/postgresql-9.3.5/data/ -m fast"

 
 
#modify allow client
/usr/local/postgresql-9.3.5/data/postgresql.conf : listen_addresses = '*'
/usr/local/postgresql-9.3.5/data/postgresql.conf : port = 5437
/usr/local/postgresql-9.3.5/data/pg_hba.conf    :     "host  all  all  192.168.0.0/16  trust"
 
##############################################
#install postgis
#postgis-2.1.4.tar.gz   proj-4.8.0.tar.gz     geos-3.4.2.tar.bz2   json-c-0.10.tar.gz   gdal-1.9.2.tar.gz
cd /root/geos-3.4.2
./configure --prefix=/usr/local/geos-3.4.2 && make && make install
echo "/usr/local/geos-3.4.2/lib/" > /etc/ld.so.conf.d/geos.conf
ldconfig && ldconfig -p | grep geos
 
cd /root/proj-4.8.0
./configure --prefix=/usr/local/proj-4.8.0 && make && make install
echo "/usr/local/proj-4.8.0/lib/" > /etc/ld.so.conf.d/proj4.conf
ldconfig && ldconfig -p | grep proj
 
cd /root/json-c-0.10
./configure --prefix=/usr/local/json-c-0.10 && make && make install
#may a bug of makefile:
cd /root/json-c-0.10 && cp config.h json_object_iterator.h /usr/local/json-c-0.10/include/json/
echo "/usr/local/json-c-0.10/lib/" > /etc/ld.so.conf.d/json-c.conf
ldconfig && ldconfig -p | grep json
 
cd /root/gdal-1.9.2
#./configure --prefix=/usr/local/gdal-1.9.2 && make && make install
#关于jpeg方面报错 先装nginx再装postgresql的 应该是利用了nginx所依赖的jpeg库的
#改成使用gdal自带的jpeg:
make clean && ./configure --prefix=/usr/local/gdal-1.9.2 --with-jpeg=internal && make && make install
echo "/usr/local/gdal-1.9.2/lib/" > /etc/ld.so.conf.d/gdal.conf
ldconfig && ldconfig -p | grep gdal

cd $pkhm
log=$loghm/postgis.log
rm -fr postgis-2.1.4 $log /usr/local/postgis-2.1.4 && tar -zxf postgis-2.1.4.tar.gz && cd postgis-2.1.4
./configure --prefix=/usr/local/postgis-2.1.4 --with-pgconfig=/usr/local/postgresql-9.3.5/bin/pg_config --with-jsondir=/usr/local/json-c-0.10 --with-geosconfig=/usr/local/geos-3.4.2/bin/geos-config --with-projdir=/usr/local/proj-4.8.0 --with-gdalconfig=/usr/local/gdal-1.9.2/bin/gdal-config 2>&1 | tee -a $log && make 2>&1 | tee -a $log && make install 2>&1 | tee -a $log 
# centos6.5下多行续行始终报错 找不到 pg_config

#test postgis, op as user postgres
/usr/local/postgresql-9.3.5/bin/psql -h wildfly.local -p 5437 -U postgres -w -d postgres -c "select * from pg_available_extensions;"
/usr/local/postgresql-9.3.5/bin/psql -h wildfly.local -p 5437 -U postgres -w -d test -c "create extension postgis;"
/usr/local/postgresql-9.3.5/bin/psql -h wildfly.local -p 5437 -U postgres -w -d test -c "create language plpython3u"